﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PokemonBattle.BattleRoom.Client;
using NetworkLib;

namespace PBO.RoomClient
{
    public partial class ChatForm : Form
    {
        private int _target;
        private string _myName;
        protected IChatClient _client;

        public ChatForm(int target, string myName, IChatClient client)
        {
            InitializeComponent();
            _target = target;
            _myName = myName;
            _client = client;
            MessageText.KeyDown += new KeyEventHandler(MessageText_KeyDown);
        }

        void MessageText_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendButton.PerformClick();
            }
        }

        internal void ReceiveChatMessage(string message)
        {
            DisplayText.InvokeAppendText(message);
        }

        private void SendButton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(MessageText.Text.Trim())) return;
            string message = string.Format("{0} : {1}", _myName, MessageText.Text);
            if (_client.Chat(_target, message))
            {
                DisplayText.InvokeAppendText(message);
                MessageText.Clear();
            }
            else
            {
                DisplayText.InvokeAppendText("对方已经不在房间中.");
            }
        }

        private void ExitButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        public int Target
        {
            get { return _target; }
        }
    }
}
